package com.cis467.mrball;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;

import com.cis467.mrball.parts.Button;
import com.cis467.mrball.parts.Level;
import com.cis467.mrball.parts.Portal;

public abstract class ScreenManager {

	protected static final int MENU_BUFFER = 2;
	
	protected Context mContext; 
	
	protected SharedPreferences mScreenPrefs;
	
	protected ImageManager mImageManager;
	
	protected PaintManager mPaintManager;
	
	// Just for saving for easy access
	protected Canvas canvas;

	/** Screen size information */
	protected int mScreenHeight;
	protected int mScreenWidth;
	protected int mBoardHeight;
	protected int mBoardWidth;
	protected int mOffsetHeight;
	protected int mOffsetWidth;
	protected int mSidePanelWidth; 
	protected int mTileSize;
	protected int mWallSize;
	protected int mSidePanelOffset;
	protected int mBannerHeight;

	public ScreenManager(Context context, boolean hasBanner) {

		mContext = context;
		
		mScreenPrefs =	mContext.getSharedPreferences(SharedConstants.SCREEN_PREFS, Context.MODE_PRIVATE);
		
		mImageManager = new ImageManager(mContext);
		mPaintManager = new PaintManager();
		
		mBannerHeight = hasBanner ? SharedConstants.BANNER_HEIGHT : 0;
	}

	public void setDimensions(int width, int height) {

		mScreenWidth = width;
		mScreenHeight = height;

		double lScreenW2HRatio = (double)(mScreenWidth - SharedConstants.MIN_SIDE_PANEL_WIDTH)/(double)(mScreenHeight - mBannerHeight);
		double lBoardW2HRatio = (double)Level.FLOOR_COLS/(double)Level.FLOOR_ROWS;

		// Determine if the width or height is going to be the limiting dimension
		if(lScreenW2HRatio > lBoardW2HRatio)
			mTileSize = (mScreenHeight - mBannerHeight)/Level.FLOOR_ROWS;						// The screen is limited by height
		else
			mTileSize = (mScreenWidth - SharedConstants.MIN_SIDE_PANEL_WIDTH)/Level.FLOOR_COLS;	// The screen is limited by width
		
		// Set the dimensions of the board
		mBoardHeight = mTileSize * Level.FLOOR_ROWS;
		mBoardWidth = mTileSize * Level.FLOOR_COLS;

		// Set the size of the side panel
		mSidePanelWidth = mScreenWidth - mBoardWidth;

		// Set the offsets so that the board is centered appropriately
		mOffsetHeight = (mBannerHeight != 0) ? mBannerHeight : (mScreenHeight - mBoardHeight) / 2;
		mOffsetWidth = (mScreenWidth - mBoardWidth - mSidePanelWidth) / 2;

		// Adjust all images to new screen size
		mImageManager.scaleImages(mTileSize);
		mImageManager.setupPixelArrays();
		
		mSidePanelOffset = mScreenWidth - mSidePanelWidth;
		
	}
	
	protected void renderLevel(Level pLevel) {
		mImageManager.renderLevel(pLevel);
	}
	
	protected void drawStatic() {
		canvas.drawBitmap(mImageManager.level, mOffsetWidth, mOffsetHeight, null);
	}

	protected void drawVisibleDynamic(Level pLevel) {

		if(pLevel.start.isVisible)
			canvas.drawBitmap(mImageManager.start, 
					pLevel.start.col*mTileSize + mOffsetWidth, 
					pLevel.start.row*mTileSize + mOffsetHeight, null);

		if(pLevel.key != null && pLevel.key.isVisible)
			canvas.drawBitmap(mImageManager.key, 
					pLevel.key.col*mTileSize + mOffsetWidth, 
					pLevel.key.row*mTileSize + mOffsetHeight, null);

		if(pLevel.end.isVisible)
			canvas.drawBitmap(mImageManager.end, 
					pLevel.end.col*mTileSize + mOffsetWidth, 
					pLevel.end.row*mTileSize + mOffsetHeight, null);    	

		for (Button b : pLevel.buttons) {
			if(b.isVisible) {
				canvas.drawBitmap(mImageManager.buttons[b.color], 
						b.col*mTileSize + mOffsetWidth, 
						b.row*mTileSize + mOffsetHeight, null);
			}
		}

		for (Portal p : pLevel.portals) {
			if(p.isVisible) {
				canvas.drawBitmap(mImageManager.portals[p.color], 
						p.col*mTileSize + mOffsetWidth, 
						p.row*mTileSize + mOffsetHeight, null);
			}
		}
	}
	
	protected void drawAllDynamic(Level pLevel) {

    	if(pLevel.start != null)
    		canvas.drawBitmap(mImageManager.start, 
    			pLevel.start.col*mTileSize + mOffsetWidth, 
    			pLevel.start.row*mTileSize + mOffsetHeight, null);
    	
    	if(pLevel.key != null)
	    	canvas.drawBitmap(mImageManager.key, 
	    			pLevel.key.col*mTileSize + mOffsetWidth, 
	    			pLevel.key.row*mTileSize + mOffsetHeight, null);

    	if(pLevel.end != null)
    		canvas.drawBitmap(mImageManager.end, 
    				pLevel.end.col*mTileSize + mOffsetWidth, 
    				pLevel.end.row*mTileSize + mOffsetHeight, null);    	
    	
    	for (Button b : pLevel.buttons) {
    		canvas.drawBitmap(mImageManager.buttons[b.color], 
    				b.col*mTileSize + mOffsetWidth, 
    				b.row*mTileSize + mOffsetHeight, null);
    	}
    	
    	for (Portal p : pLevel.portals) {
    		canvas.drawBitmap(mImageManager.portals[p.color], 
    				p.col*mTileSize + mOffsetWidth, 
    				p.row*mTileSize + mOffsetHeight, null);
    	}
    }
    
    protected  void clearCanvas() {
		if(canvas != null && mPaintManager != null)
			canvas.drawRect(0, 0, mScreenWidth, mScreenHeight, mPaintManager.getPaint(PaintManager.PANEL));
	}
}
